﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using JYStockAnalyser.Model;
using JYStockAnalyser.Face;
using JYStockAnalyser.ClientLib;
using ZedGraph;

namespace JYStockAnalyser.WinUI
{
    public partial class FrmFinancialMultipleStocksAnalyse : FrmFunctionBase
    {
        public FrmFinancialMultipleStocksAnalyse()
        {
            InitializeComponent();     
        }

        private void FrmFinancialMultipleStocksAnalyse_Load(object sender, EventArgs e)
        {
            if (!this.DesignMode)
            {
                m_init();
            }
        }

        private void m_init()
        {
            this.stockInputer1.BindData();

            StyleManager.SetStyle(this.zedGraphControl1);
            this.zedGraphControl1.GraphPane.Legend.Position = LegendPos.TopFlushLeft;
            this.zedGraphControl1.GraphPane.Legend.FontSpec.Size = 14F;
            this.zedGraphControl1.GraphPane.Legend.IsShowLegendSymbols = true;

            this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.FontColor = Color.White;
            this.zedGraphControl1.GraphPane.XAxis.Scale.FontSpec.Size = 14F;
            this.zedGraphControl1.GraphPane.Y2Axis.Scale.FontSpec.FontColor = Color.White;
            this.zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.Size = 14F;
           

            string[] reports = ClientCache.Instance.FinancialCategories.ToArray();
            foreach (string report in reports)
            {
                this.ddlReports.Items.Add(report);
            }
            this.ddlReports.SelectedIndex = 0;

            this.stockInputer1.Focus();
        }

        private void m_bindSelectedReportGuideLines()
        {
            this.ddlGuideLines.Items.Clear();
            this.ddlGuideLines.Items.Add("-请选择-");
            if (this.ddlReports.SelectedIndex == -1)
                return;

            List<GuideLineType> infos = ClientCache.Instance.AllFinancialGuideLineTypes[(string)this.ddlReports.SelectedItem][2];
            foreach (GuideLineType info in infos)
            {
                this.ddlGuideLines.Items.Add(info);
            }
            this.ddlGuideLines.SelectedIndex = 0;
        }

        private void ddlReports_SelectedIndexChanged(object sender, EventArgs e)
        {
            m_bindSelectedReportGuideLines();
        }

        private void btnAddStock_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.stockInputer1.SelectedStockNO))
                return;

            System.Windows.Forms.Label lbl = new System.Windows.Forms.Label();
            lbl.Text = String.Format("{0}({1})", this.stockInputer1.SelectedStockName, this.stockInputer1.SelectedStockNO);
            lbl.Tag = this.stockInputer1.SelectedStockNO;
            lbl.ForeColor = Color.Yellow;
            lbl.Width = 150;
            lbl.AutoSize = false;
            lbl.Font = new Font("宋体", 11f);



            System.Windows.Forms.Button btn = new Button();
            btn.Text = "×";
            btn.FlatStyle = FlatStyle.Flat;
            btn.ForeColor = Color.White;
            btn.Cursor = Cursors.Hand;
            btn.Tag = lbl;
            btn.Width = 20;
            btn.Click += new EventHandler(btn_Click);

            this.plStocks.Controls.Add(btn);
            this.plStocks.Controls.Add(lbl);
            this.plStocks.Refresh();
        }

        void btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            this.plStocks.Controls.Remove((System.Windows.Forms.Label)btn.Tag);
            this.plStocks.Controls.Remove(btn);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            if (this.plStocks.Controls.Count == 0)
            {
                Tool.ShowInformationBox("请至少选择一支股票!");
                this.btnAddStock.Focus();
                return;
            }

            if (this.ddlGuideLines.SelectedIndex == -1 || this.ddlGuideLines.SelectedIndex == 0)
            {
                Tool.ShowInformationBox("请选择一个指标!");
                this.ddlGuideLines.Focus();
                return;
            }


            GuideLineType selectedGuideLine = (GuideLineType)this.ddlGuideLines.SelectedItem;
            string analyseTimeType = "年";
            YearQuarter beginYear = this.periodInputer1.BeginQuarter;
            YearQuarter endYear = this.periodInputer1.EndQuarter;
            List<string> stockNos = new List<string>();
            for (int i = 1; i < this.plStocks.Controls.Count; i += 2)
            {
                stockNos.Add((string)((System.Windows.Forms.Label)this.plStocks.Controls[i]).Tag);
            }

            object[] objs = new StockManageFace().SearchGuideLines(stockNos.ToArray(), analyseTimeType, beginYear, endYear, selectedGuideLine);


            GraphPane myPane = this.zedGraphControl1.GraphPane;
            myPane.CurveList.Clear();

            for (int i = 0; i < objs.Length; i++)
            {
                List<GuideLine> searchedlines = (List<GuideLine>)objs[i];
                GuideLinesCollator.CollateLines(analyseTimeType, beginYear, endYear, selectedGuideLine, searchedlines);


                if (i == 0)
                {
                    List<string> strs = new List<string>();
                    foreach (GuideLine line in searchedlines)
                    {
                        strs.Add(line.GetTimeSimpleString());
                    }

                    myPane.XAxis.Scale.TextLabels = strs.ToArray();
                }

                List<double> values = new List<double>();

                foreach (GuideLine line in searchedlines)
                {
                    values.Add((double)line.Value);
                }

                myPane.XAxis.Type = AxisType.Text;

                LineItem myCurve = myPane.AddCurve(((System.Windows.Forms.Label)this.plStocks.Controls[i * 2 + 1]).Text, null, values.ToArray(), Color.Yellow);
                StyleManager.SetCommonLineItemStyle(myCurve);
                myCurve.Line.Color = StyleManager.GetLineColor(i - 1);
                myCurve.Symbol.Fill = new Fill(StyleManager.GetSymbolColor(i - 1));
                myCurve.Symbol.Border.Color = myCurve.Line.Color;

                this.zedGraphControl1.AxisChange();
                zedGraphControl1.Refresh();
            }
        }
    }
}
